﻿namespace WMS6.Web.CrossDock
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CrossdockServiceRef;

    public class FlowThruAllocation : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected DropDownList ddl;
        protected HtmlForm F79621;
        protected GridView gvApportionmentRule;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlHead Head1;
        protected HtmlImage imgOwnerID;
        protected Label lbl;
        protected Label lblApportionmentRule;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtOwner;
        protected TextBox txtOwnerID;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = "请输入货主!";
            }
            else
            {
                base.Response.Redirect(string.Format("FlowThruApportionmentRuleDetail.aspx?from={0}&ID={1}", this.hdnFrom.Value, this.txtOwnerID.Text.Trim()), true);
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtOwnerID.Text = "";
            this.txtOwnerID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> OwnerIDs = new List<string> {
                    this.txtOwnerID.Text
                };
                this.lblMsg.Text = CrossdockServiceClientProxy.GetInstance().DeleteApportionRuleInfo(OwnerIDs, null, null) + "删除成功!";
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> OwnerIDs = base.GetSelectedIDsInGridView(this.gvApportionmentRule, "OWNERID");
                List<string> CustomerIDs = base.GetSelectedIDsInGridView(this.gvApportionmentRule, "CUSTOMERID");
                List<string> SeqNo = base.GetSelectedIDsInGridView(this.gvApportionmentRule, "SEQNO");
                this.lblMsg.Text = CrossdockServiceClientProxy.GetInstance().DeleteApportionRuleInfo(OwnerIDs, CustomerIDs, SeqNo) + base.GetGlobalMsgResource("C006");
                this.LoadData();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("FlowThruApportionmentRuleList.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("FlowThruApportionmentRuleList.aspx?from=edit", false);
                    break;
            }
        }

        protected void gvApportionmentRule_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F79621", this.gvApportionmentRule);
        }

        protected void gvApportionmentRule_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvApportionmentRule.PageIndex * this.gvApportionmentRule.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnOwnerID.Value = base.Request.QueryString["ID"].ToString();
                this.SetApportionmentRule2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetApportionmentRule2Form()
        {
            try
            {
                string OwnerID = this.hdnOwnerID.Value;
                this.txtOwnerID.Text = OwnerID;
                this.txtOwnerID.Enabled = false;
                List<ApportionRuleInfo> apportionRuleInfos = CrossdockServiceClientProxy.GetInstance().GetApportionRuleListByID(OwnerID);
                base.DataEntityViewState = apportionRuleInfos;
                if ((apportionRuleInfos != null) && (apportionRuleInfos.Count > 0))
                {
                    base.BindData2GridView(this.gvApportionmentRule, apportionRuleInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

